package net.hvacapp.cmvrfforandroid.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.hvacapp.cmvrfforandroid.R;

/**
 * IOS风格的弹出框(高仿UIAlertView)
 *
 * @author hailong.han
 * @description 2014年8月29日
 */
public class IOSAlertView {

    private static TextView titleTextView;
    private static TextView messageTextView;

    /**
     * 自定义IOS 弹出框
     *
     * @param activity
     * @param title
     * @param message
     * @param positiveButton
     * @param buttonClickListener
     * @param otherButtons
     * @return
     */
    public static Dialog showAlertView(Activity activity, String title,
                                       String message, String positiveButton,
                                       final OnAlertViewClickListener buttonClickListener,
                                       String... otherButtons) {
        return showAlertView(activity, title, message, positiveButton,
                buttonClickListener, null, otherButtons);
    }

    /**
     * 自定义标题头背景色和字体色、内容显示位置的弹出框
     *
     * @param activity
     * @param title
     * @param titleBgColor
     * @param titleTextColor
     * @param message
     * @param positiveButton
     * @param buttonClickListener
     * @param onAlertBackClickListener
     * @param otherButtons
     * @return
     */
    public static Dialog showAlertView(Activity activity, String title,
                                       int titleBgColor, int titleTextColor, String message,
                                       int messageGravity, String positiveButton,
                                       final OnAlertViewClickListener buttonClickListener,
                                       final OnAlertBackClickListener onAlertBackClickListener,
                                       String... otherButtons) {

        Dialog dialog = showAlertView(activity, title, message, positiveButton,
                buttonClickListener, onAlertBackClickListener, otherButtons);
        if (titleTextView != null) {
            RoundRectShape titleRoundRectShape = new RoundRectShape(
                    new float[]{10, 10, 10, 10, 0, 0, 0, 0}, null, null);
            ShapeDrawable titleShapeDrawable = new ShapeDrawable(
                    titleRoundRectShape);
            titleShapeDrawable.getPaint().setColor(titleBgColor);

            if (Build.VERSION.SDK_INT >= 16) {
                titleTextView.setBackground(titleShapeDrawable);
            } else {
                titleTextView.setBackgroundDrawable(titleShapeDrawable);
            }
            titleTextView.setTextColor(titleTextColor);
        }
        if (messageTextView != null) {
            messageTextView.setGravity(messageGravity);
        }

        return dialog;
    }

    /**
     * 自定义内容显示位置的弹出框
     *
     * @param activity
     * @param title
     * @param message
     * @param positiveButton
     * @param buttonClickListener
     * @param onAlertBackClickListener
     * @param otherButtons
     * @return
     */
    public static Dialog showAlertView(Activity activity, String title,
                                       String message, int messageGravity, String positiveButton,
                                       final OnAlertViewClickListener buttonClickListener,
                                       final OnAlertBackClickListener onAlertBackClickListener,
                                       String... otherButtons) {

        Dialog dialog = showAlertView(activity, title, message, positiveButton,
                buttonClickListener, onAlertBackClickListener, otherButtons);
        if (messageTextView != null) {
            messageTextView.setGravity(messageGravity);
        }

        return dialog;
    }

    /**
     * 点击返回键的IOS 弹出框
     *
     * @param activity
     * @param title
     * @param message
     * @param positiveButton
     * @param buttonClickListener
     * @param onAlertBackClickListener
     * @param otherButtons
     * @return
     */
    public static Dialog showAlertView(Activity activity, String title,
                                       String message, String positiveButton,
                                       final OnAlertViewClickListener buttonClickListener,
                                       final OnAlertBackClickListener onAlertBackClickListener,
                                       String... otherButtons) {
        final Dialog dialog = new Dialog(activity, R.style.alertView);

        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = (View) inflater.inflate(R.layout.alertview, null);

        titleTextView = (TextView) view.findViewById(R.id.titleTextView);
        messageTextView = (TextView) view.findViewById(R.id.messageTextView);
        if (null != title && !title.equalsIgnoreCase("")) {
            titleTextView.setText(title);
        } else {
            titleTextView.setVisibility(View.GONE);
        }
        if (null != message && !message.equalsIgnoreCase("")) {
            messageTextView.setText(message);
        } else {
            messageTextView.setVisibility(View.GONE);
        }

        Button pButton = new Button(activity);
        pButton.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, dip2px(activity, 44), 1.0f));
        pButton.setTextColor(Color.argb(255, 0, 121, 255));

        // 动态添加按钮
        LinearLayout buttonLayout = (LinearLayout) view
                .findViewById(R.id.buttonLayout);

        if (null == otherButtons || otherButtons.length == 0) {
            // 一个按钮
            buttonLayout.setOrientation(LinearLayout.VERTICAL);

            pButton.setBackgroundResource(R.drawable.alert_bottom_button);

        } else if (null != otherButtons && otherButtons.length == 1) {
            // 两个按钮
            buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
            pButton.setBackgroundResource(R.drawable.alert_left_button);
        } else {
            // 三个或三个以上按钮
            buttonLayout.setOrientation(LinearLayout.VERTICAL);
            pButton.setBackgroundResource(R.drawable.alert_middle_button);
        }

        pButton.setText(positiveButton);
        pButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != buttonClickListener) {
                    buttonClickListener.OnAlertViewClick(0);
                }
                dialog.dismiss();
            }

        });

        buttonLayout.addView(pButton);

        if (null != otherButtons && otherButtons.length > 0) {
            for (int i = 0; i < otherButtons.length; i++) {
                final int index = i + 1;
                Button otherButton = new Button(activity);
                otherButton.setLayoutParams(new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, dip2px(activity, 44), 1.0f));
                otherButton.setText(otherButtons[i]);
                otherButton.setTextColor(Color.argb(255, 0, 121, 255));
                if (1 == otherButtons.length) {
                    otherButton
                            .setBackgroundResource(R.drawable.alert_right_button);
                } else if (i < (otherButtons.length - 1)) {
                    otherButton
                            .setBackgroundResource(R.drawable.alert_middle_button);
                } else {
                    otherButton
                            .setBackgroundResource(R.drawable.alert_bottom_button);
                }
                otherButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (null != buttonClickListener) {
                            buttonClickListener.OnAlertViewClick(index);
                        }
                        dialog.dismiss();
                    }

                });

                buttonLayout.addView(otherButton);
            }
        }

        dialog.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    if (onAlertBackClickListener != null) {
                        onAlertBackClickListener.onAlertBackClick();
                    }
                    return true;
                }
                return false;
            }
        });

        final int viewWidth = dip2px(activity, 250);
        view.setMinimumWidth(viewWidth);

        dialog.setCanceledOnTouchOutside(false);

        dialog.setContentView(view);
        dialog.show();
        return dialog;
    }

    public interface OnAlertViewClickListener {
        void OnAlertViewClick(int position);
    }

    public interface OnAlertBackClickListener {
        void onAlertBackClick();
    }

    private static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
